package cn.kgm.article.server.controller;

import cn.kgm.article.dto.dto.ArticleInfoSaveDTO;
import cn.kgm.article.dto.dto.ArticleInfoUpdateDTO;
import cn.kgm.article.dto.query.ArticleInfoPageQuery;
import cn.kgm.article.dto.vo.ArticleInfoVO;
import cn.kgm.article.server.entity.ArticleInfo;
import cn.kgm.article.server.service.ArticleInfoService;
import cn.kgm.article.server.struct.ArticleInfoMappers;
import cn.kgm.core.http.request.BaseController;
import cn.kgm.core.http.response.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * # 技术改变自己 #
 * @author kapi
 * @date 2024-03-15 14:33:45
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/articleInfo")
@Api(value = "ArticleInfo", tags = "文章详情")
public class ArticleInfoController extends BaseController {

    @Autowired
    private ArticleInfoService articleInfoService;

    /**
     * 分页列表查询文章详情
     */
    @ApiOperation(value = "分页查询", notes = "分页查询文章详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataType = "long", paramType = "query", defaultValue = "20"),
    })
    @GetMapping(value = "/page")
    public R<IPage<ArticleInfoVO>> page(ArticleInfoPageQuery data) {
        IPage<ArticleInfo> page = getPage();
        // 构建不为null的查询条件
        ArticleInfo articleInfo = ArticleInfoMappers.INSTANCE.mapToEntity(data);
        LambdaQueryWrapper<ArticleInfo> wrapper = Wrappers.lambdaQuery(articleInfo);
        // 分页查询
        articleInfoService.page(page, wrapper);
        return R.success(page.convert((ArticleInfoMappers.INSTANCE::mapToEntity)));
    }

    /**
     * 根据id查询文章详情详情
     */
    @ApiOperation(value = "查看详情", notes = "根据id查询文章详情详情")
    @GetMapping(value = "/{id}")
    public R<ArticleInfoVO> get(@PathVariable(value = "id") Long id) {
        ArticleInfo articleInfo = articleInfoService.getById(id);
        return R.success(ArticleInfoMappers.INSTANCE.mapToEntity(articleInfo));
    }

    /**
     * 新增文章详情
     */
    @ApiOperation(value = "新增", notes = "新增文章详情")
    @PostMapping("/save")
    public R<Boolean> save(@RequestBody @Validated ArticleInfoSaveDTO data) {
        ArticleInfo articleInfo = ArticleInfoMappers.INSTANCE.mapToEntity(data);
        articleInfoService.save(articleInfo);
        return R.success();
    }

    /**
     * 修改文章详情
     */
    @ApiOperation(value = "修改", notes = "修改文章详情")
    @PostMapping("/update")
    public R<Boolean> update(@RequestBody @Validated ArticleInfoUpdateDTO data) {
        ArticleInfo articleInfo = ArticleInfoMappers.INSTANCE.mapToEntity(data);
        articleInfoService.updateById(articleInfo);
        return R.success();
    }

    /**
     * 根据id更新文章详情的删除状态
     */
    @ApiOperation(value = "删除", notes = "根据id更新文章详情的删除状态")
    @GetMapping(value = "/delete/{ids}")
    public R<Boolean> delete(@PathVariable(value = "ids") Long[] ids) {
        articleInfoService.update(Wrappers.<ArticleInfo>lambdaUpdate()
                .set(ArticleInfo::getIsDel, true)
                .in(ArticleInfo::getId, ids));
        return R.success();
    }
}
